// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <memory>
#include <optional>
#include <string>
#include <vector>

namespace kumo::pollux::exec {
    // Base type (e.g. map) and optional parameters (e.g. K, V).
    class TypeSignature {
    public:
        /// @param baseName The base name of the type. Could describe a concrete type
        /// name (e.g. map, bigint, double), or a variable (e.g. K, V).
        /// @param parameters The optional parameters for the type. For example, the
        /// signature "map(K, V)" would have two parameters, "K", and "V". All
        /// parameters must be of the same ParameterType.
        /// @param rowFieldName if this type signature is a field of another parent
        /// row type, it can optionally have a name. E.g. `row(id bigint)` would have
        /// "id" set as rowFieldName in the "bigint" parameter.
        TypeSignature(
            std::string baseName,
            std::vector<TypeSignature> parameters,
            std::optional<std::string> rowFieldName = std::nullopt)
            : baseName_{std::move(baseName)},
              parameters_{std::move(parameters)},
              rowFieldName_(rowFieldName) {
        }

        const std::string &baseName() const {
            return baseName_;
        }

        const std::vector<TypeSignature> &parameters() const {
            return parameters_;
        }

        const std::optional<std::string> &rowFieldName() const {
            return rowFieldName_;
        }

        std::string toString() const;

        bool operator==(const TypeSignature &rhs) const {
            return baseName_ == rhs.baseName_ && parameters_ == rhs.parameters_ &&
                   rowFieldName_ == rhs.rowFieldName_;
        }

    private:
        const std::string baseName_;
        const std::vector<TypeSignature> parameters_;

        // If this object is a field of another parent row type, it can optionally
        // have a name, e.g, `row(id bigint)`
        std::optional<std::string> rowFieldName_;
    };

    using TypeSignaturePtr = std::shared_ptr<TypeSignature>;
} // namespace kumo::pollux::exec
